package com.tang.Utils;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbcp2.BasicDataSource;
import sun.security.util.Password;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
    private static ThreadLocal<Connection> tl=new ThreadLocal<Connection>();
    public static Connection getThreadConnetion() throws SQLException {
        Connection con=tl.get();
        if(con==null){
            con=getDBConnection();
            con.setAutoCommit(false);
            tl.set(con);
        }
        return con;
    }
    public static void commitAndClose() throws SQLException {
        Connection con=getThreadConnetion();
        if(con!=null){
            try {
                con.commit();
            }catch(SQLException e){
                e.printStackTrace();
            }finally{
                try {
                    con.close();
                }catch(SQLException ee){
                    ee.printStackTrace();
                }
            }
        }
        tl.remove();
    }
    public static void rollbackAndClose() throws SQLException {
        Connection con=getThreadConnetion();
        if(con!=null){
            try {
                con.rollback();
            }catch(SQLException e){
                e.printStackTrace();
            }finally{
                try {
                    con.close();
                }catch(SQLException ee){
                    ee.printStackTrace();
                }
            }
        }
        tl.remove();
    }
    public static Connection getDBConnection(){
        String name="root";
        String password="1234";
        String url="jdbc:mysql://localhost:3306/bookstore?useSSL=true&characterEncoding=utf-8";
        Connection con=null;
       try{
           Class.forName("com.mysql.jdbc.Driver");
       }catch(ClassNotFoundException e){
           e.printStackTrace();
       }
       try{
          con=DriverManager.getConnection(url,name,password);
       }catch(SQLException e){
           e.printStackTrace();
       }
        return con;
    }
    public static void closeCon(Connection con){
        if(con!=null) {
            try {
                con.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }
    public static DruidDataSource getDataSourse(){
        Properties pros = new Properties();
        DruidDataSource dds = new DruidDataSource();
        try {
            InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            pros.load(is);
        }catch(IOException e){
            e.printStackTrace();
        }
        try {
            dds=(DruidDataSource)DruidDataSourceFactory.createDataSource(pros);
            System.out.println("数据库连接成功");
        }catch(Exception e){
            System.out.println("数据库连接失败");
            e.printStackTrace();
        }
        //dds=(DruidDataSource)DruidDataSourceFactory.createDataSource();
        return dds;
    }

}